#include <clutter/clutter.h>
#include <stdlib.h>

#define PER 0.2

ClutterActor *rect;
ClutterActor *stage;

ClutterColor stage_color = {0x00, 0x00, 0x00, 0xff};
ClutterColor actor_color = {0xff, 0xff, 0xff, 0x99};

ClutterAction *action;

gfloat stagew, stageh;
gfloat rectw = 20;
gfloat recth = 20;

static gboolean
stage_resize_cb (ClutterStage *stage, ClutterEvent *event, gpointer data)
{
	gfloat stagewn, stagehn;
	clutter_actor_get_size (stage, &stagewn, &stagehn);

//	clutter_actor_set_size (rect, stagewn * PER, stagehn *PER);

	clutter_actor_set_position (rect, stagewn/2, stagehn/2);
}

static gboolean
on_rect_button_press (ClutterRectangle *rect, ClutterEvent *event, gpointer data)
{
	gfloat posx, posy;
	clutter_event_get_coords (event, &posx, &posy);

//	clutter_actor_set_position (rect, posx, posy);


}

static gboolean
on_rect_drag (ClutterRectangle *rect, ClutterEvent *event, gpointer data)
{
	gfloat posx, posy;
	clutter_event_get_coords (event, &posx, &posy);
	g_print("posx:%lf,posy:%d\n",posx, posy);

	clutter_actor_set_position (rect, posx, posy);
}

static gboolean
on_rect_button_release (ClutterRectangle *rect, ClutterEvent *event, gpointer data)
{
}

int main (int argc, char *argv[])
{
	clutter_init (&argc, &argv);

	stagew = stageh = 500;

	stage = clutter_stage_get_default ();
	clutter_actor_set_size (stage, stagew, stageh);
	clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color);
	clutter_stage_set_user_resizable (CLUTTER_STAGE (stage), TRUE);

	g_signal_connect (stage, "allocation-changed", G_CALLBACK (stage_resize_cb), NULL);



	rect = clutter_rectangle_new_with_color (&actor_color);
	clutter_actor_set_size (rect, rectw, recth);
	clutter_actor_set_position (rect, stagew/2 , stageh/2 );
	clutter_container_add_actor (CLUTTER_CONTAINER (stage), rect);

	clutter_actor_set_reactive (rect, TRUE);

	g_signal_connect (rect, "button-press-event", G_CALLBACK (on_rect_button_press), NULL);
	g_signal_connect (rect, "button-release-event", G_CALLBACK (on_rect_button_release), NULL);

	action = clutter_drag_action_new ();

	g_signal_connect (action, "drag", G_CALLBACK (on_rect_drag), NULL);
	clutter_actor_add_action (rect, action);

	clutter_actor_show (rect);
	clutter_actor_show (stage);

	clutter_main ();

	return EXIT_SUCCESS;
}
